/**
 * 查询文章详情
 */

import { AppDataSource } from '../../db/index'
import { Article } from '../../db/entities/Article'

const articleRepository = AppDataSource.getRepository(Article)

export default defineEventHandler(async (event) => {
  let id = getRouterParam(event, 'id')

  if (!id) {
    return {
      code: 1000,
      data: null,
      message: '文章不存在'
    }
  }

  let row = await articleRepository.findOne({
    select: {
      // ts-ignore
      tags: {
        tag_id: true,
        name: true
      }
    },
    where: {
      id: id
    },
    relations: ['tags']
  })

  return {
    code: 0,
    data: row,
    message: 'success'
  }
})
